<?php
// +---------------------------------------------------------------------+
// | ALINEJUN    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/ALINEJUN                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 省市县三级联动小物件逻辑
 */
class Region extends AdminBase
{

    /**
     * 组合下拉框选项信息
     */
    public function combineOptions($id = 0, $list = [], $default_option_text = '')
    {
        
        $data = "<option value =''>$default_option_text</option>";
        
        foreach ($list as $vo)
        {
            $data .= "<option ";
            
            !($id == $vo['id']) ?: $data .= " selected ";
            
            $data .= " value ='" . $vo['id'] . "'>" . $vo['name'] . "</option>";
        }
        
        return $data;
    }
    
    /**
     * 获取省市县选项信息
     */
    public function getRegionOptions($upid = 0, $level = 1, $select_id = 0)
    {
        
        $list = $this->getRegionList(['upid' => $upid, 'level' => $level]);
        
        switch ($level)
        {
            case 1: $default_option_text = "---请选择省份---"; break;
            case 2: $default_option_text = "---请选择城市---"; break;
            case 3: $default_option_text = "---请选择区县---"; break;
            default: $this->error('省市县 level 不存在');
        }
        
        return $this->combineOptions($select_id, $list, $default_option_text);
    }
    
    /**
     * 获取区域列表
     */
    public function getRegionList($where = [])
    {
        
        $cache_key = 'cache_region_' . md5(serialize($where));
        
        $cache_list = cache($cache_key);
        
        if (!empty($cache_list)) {
            
            return $cache_list;
        }
        
        $list = $this->modelRegion->getList($where, true, 'id', false);
        
        !empty($list) && cache($cache_key, $list);
        
        return $list;
    }

    /**
     * 根据省名，获取id
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProvinceIdByName($name)
    {
        $res = $this->modelRegion->where('name',$name)->find();
        if($res){
            $res = $res->toArray();
        }
        return  $res;
    }

    /**
     * 根据省id，城市名，获取城市id
     * @param $province_id
     * @param $city
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCityIdByName($province_id,$city)
    {
        $res = $this->modelRegion->where(['upid'=>$province_id,'name'=>$city])->find();
        if($res){
            $res = $res->toArray();
        }
        return  $res;
    }

    /**
     * 根据省id，城市名，获取城市id
     * @param $province_id
     * @param $city
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCountyIdByName($county_id,$county)
    {
        $res = $this->modelRegion->where(['upid'=>$county_id,'name'=>$county])->find();
        if($res){
            $res = $res->toArray();
        }
        return $res ;
    }

    /**
     * 根据id 查找数据信息
     */
    public function getInfoById($id=0)
    {
        if(!$id){
            return '';
        }
        $res = $this->modelRegion->find($id);
        return $data = $res ? $res['name'] : '';
    }
}
